#!/bin/bash

action=$1
target=$2
args=$@

BASE_DIR=directory
ORIGINAL_PORT=port
ORIGINAL_VERSION=version
ORIGINAL_ENTRANCE=entrance
ORIGINAL_USERNAME=username
ORIGINAL_PASSWORD=password
LANGUAGE=en

if [ -f "/usr/local/bin/lang/$LANGUAGE.sh" ]; then
    source "/usr/local/bin/lang/$LANGUAGE.sh"
else
    LANGUAGE=en
fi

# 检查是否是root用户，避免多次要求输入密码
check_root() {
    if [[ $EUID -ne 0 ]]; then
        echo "$TXT_RUN_AS_ROOT"
        exit 1
    fi
}

function usage() {
    echo "$PANEL_CONTROL_SCRIPT"
    echo
    echo "Usage: "
    echo "  ./1pctl [COMMAND] [ARGS...]"
    echo "  ./1pctl --help"
    echo
    echo "Commands: "
    echo "  status              $TXT_PANEL_SERVICE_STATUS"
    echo "  start               $TXT_PANEL_SERVICE_START"
    echo "  stop                $TXT_PANEL_SERVICE_STOP"
    echo "  restart             $TXT_PANEL_SERVICE_RESTART"
    echo "  uninstall           $TXT_PANEL_SERVICE_UNINSTALL"
    echo "  user-info           $TXT_PANEL_SERVICE_USER_INFO"
    echo "  listen-ip           $TXT_PANEL_SERVICE_LISTEN_IP"
    echo "  version             $TXT_PANEL_SERVICE_VERSION"
    echo "  update              $TXT_PANEL_SERVICE_UPDATE"
    echo "  reset               $TXT_PANEL_SERVICE_RESET"
    echo "  restore             $TXT_PANEL_SERVICE_RESTORE"
}

_service_manager() {
    local service_name="1panel"
    if command -v systemctl &>/dev/null; then
        service_manager="systemctl"
        service_target="${service_name}.service"
    elif command -v rc-service &>/dev/null; then
        service_manager="openrc"
        service_target="${service_name}d"
    else
        service_manager="sysvinit"
        service_target="${service_name}d"
    fi
}

_service_cmd() {
    local cmd=$1 success_msg=$2 error_msg=$3
    _service_manager
    case $service_manager in
    systemctl)
        if ! systemctl "$cmd" "$service_target" 2>/dev/null; then
            echo "$error_msg" >&2
            exit 1
        fi
        ;;
    openrc)
        if ! rc-service "$service_target" "$cmd" 2>/dev/null; then
           echo "$error_msg" >&2
           exit 1
        fi
        ;;
    *)
        if ! service "$service_target" "$cmd" 2>/dev/null; then
           echo "$error_msg" >&2
           exit 1
        fi
        ;;
    esac
    [[ -n "$success_msg" ]] && echo "$success_msg"
}
_safe_remove() {
    local path=$1
    if [[ -e "$path" || -L "$path" ]]; then 
        rm -rf -- "$path" || {
            echo "Failed to remove: $path" >&2
            exit 1
        }
    fi
}

function status() {
    _service_cmd "status" "" ""
}

function start() {
    _service_cmd "start" "$TXT_PANEL_SERVICE_START_SUCCESS" "$TXT_PANEL_SERVICE_START_ERROR"
}

function stop() {
    _service_cmd "stop" "$TXT_PANEL_SERVICE_STOP $TXT_SUCCESS_MESSAGE" "$TXT_PANEL_SERVICE_STOP $TXT_FAILED_MESSAGE"
}

function restart() {
    _service_cmd "restart" "$TXT_PANEL_SERVICE_RESTART $TXT_SUCCESS_MESSAGE" "$TXT_PANEL_SERVICE_RESTART $TXT_FAILED_MESSAGE"
}

function uninstall() {
    check_root
    local yn
    read -p "$TXT_PANEL_SERVICE_UNINSTALL_NOTICE : " yn
    case "${yn,,}" in  
        y)
            echo "$TXT_PANEL_SERVICE_UNINSTALL_START"
            _service_manager          
            echo -e "1) $TXT_PANEL_SERVICE_UNINSTALL_STOP"
            case $service_manager in
                systemctl)
                    systemctl stop 1panel.service
                    systemctl disable 1panel.service >/dev/null 2>&1
                    ;;
                openrc)
                    rc-service 1paneld stop
                    rc-update del 1paneld >/dev/null 2>&1
                    ;;
                *)
                    service 1paneld stop
                    ;;
            esac
            echo -e "2) $TXT_PANEL_SERVICE_UNINSTALL_REMOVE"
            _safe_remove "${BASE_DIR}/1panel"
            _safe_remove "/usr/local/bin/1pctl" 
            _safe_remove "/usr/local/bin/1panel"
            _safe_remove "/usr/local/bin/lang"
            _safe_remove "/usr/bin/1panel"
            _safe_remove "/usr/bin/1pctl"
            echo -e "3) $TXT_PANEL_SERVICE_UNINSTALL_REMOVE_CONFIG"
            case $service_manager in
                systemctl)
                    _safe_remove "/etc/systemd/system/1panel.service"
                    systemctl daemon-reload
                    systemctl reset-failed
                    ;;
                openrc)
                    _safe_remove "/etc/init.d/1paneld"
                    ;;
                *)
                    _safe_remove "/etc/init.d/1paneld"
                    ;;
            esac
            echo -e "4) $TXT_PANEL_SERVICE_UNINSTALL_REMOVE_SUCCESS"
            ;;
        *)
            exit 0
            ;;
    esac
}
function user-info() {
    1panel -l $LANGUAGE user-info
}

function listen-ip() {
    case "${target}" in
        ipv4)
            1panel -l $LANGUAGE listen-ip ipv4
            restart
            ;;
        ipv6)
            1panel -l $LANGUAGE listen-ip ipv6
            restart
            ;;
        *)
            1panel -l $LANGUAGE listen-ip
            ;;
    esac
}

function restore() {
    read -p "$TXT_PANEL_SERVICE_RESTORE_NOTICE : " yn
    if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
        echo -e ""
        1panel -l $LANGUAGE restore
        if command -v systemctl &> /dev/null; then
            systemctl daemon-reload
        fi
        restart
        1panel -l $LANGUAGE version
    else
        exit 0
    fi
}

function version() {
    1panel -l $LANGUAGE version
}

function reset() {
    case "${target}" in
        domain)
            1panel -l $LANGUAGE reset domain
            ;;
        entrance)
            1panel -l $LANGUAGE reset entrance
            ;;
        https)
            1panel -l $LANGUAGE reset https
            restart
            ;;
        ips)
            1panel -l $LANGUAGE reset ips
            ;;
        mfa)
            1panel -l $LANGUAGE reset mfa
            ;;
        *)
            1panel -l $LANGUAGE reset
            ;;
    esac
}

function update() {
    case "${target}" in
        username)
            1panel -l $LANGUAGE update username
            ;;
        password)
            1panel -l $LANGUAGE update password
            ;;
        port)
            1panel -l $LANGUAGE update port
            ;;
        *)
            1panel -l $LANGUAGE update
            ;;
    esac
}

function main() {
    case "${action}" in
        status)
            status
            ;;
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            restart
            ;;
        restore)
            restore
            ;;
        uninstall)
            uninstall
            ;;
        user-info)
            user-info
            ;;
        listen-ip)
            listen-ip
            ;;
        version)
            version
            ;;
        reset)
            reset
            ;;
        update)
            update
            ;;
        help)
            usage
            ;;
        --help)
            usage
            ;;
        "")
            usage
            ;;
        *)
            echo "$TXT_PANEL_SERVICE_UNSUPPORTED_PARAMETER"
            ;;
    esac
}

main

